package ua.kpi.apolonov.tester;

import java.util.ArrayList;
import java.util.List;
import ua.kpi.apolonov.IO.ExternalFileService;
import ua.kpi.apolonov.IO.InputOutput;
import ua.kpi.apolonov.answers.Answer;
import ua.kpi.apolonov.questions.Question;
import ua.kpi.apolonov.users.UserClient;

/**
 * This class manages testing process
 * @version 4.3 15.03.2013
 * @author Yegor Apolonov
 *
 */

public class Examinant {
    
    private float result = 0;
    private float maxScore = 0;
    private List<Question> questions;
    private UserClient user;
        
    public Examinant(List<Question> questions) {
        this.questions = new ArrayList<Question>(questions);
        calculateMaxScore();
    } 
    
    public void performTest() {
        InputOutput dialog = new InputOutput(System.in, System.out);
        dialog.helloUser();
        user = new UserClient(dialog.inputAnswers());
        while(new ExternalFileService(user.getClientName()).isFileExists()) {
        	user = new UserClient(dialog.reenterUserName());
        }
        
        for(Question question: questions) {
        	dialog.outQuestionBlock(question);
        	String userAnswers = dialog.inputAnswers();
        	float questionResult = question.inspectAnswers(userAnswers);
        	calculateResult(questionResult);
        }
    }
        
    public float getResult() {
        return result;
    }
        
    public float getMaxScore() {
        return maxScore;
    }
        
    public String getUserName() {
    	return user.getClientName();
    }
    
    private void calculateResult(float questionResult) {
        this.result += questionResult;
    }
        
    private void calculateMaxScore() {
        for(Question question : questions) {
        	for(Answer allAnswers : question) {
        		if(allAnswers.getCorrectness()) {
        			this.maxScore++;
        		}
        	}
        }
    }
       
}
